package com.sfh.dog.ui.fragment;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.FindListener;

import com.sfh.dog.DogApplication;
import com.sfh.dog.R;
import com.sfh.dog.adapter.UserDailyAdapter;
import com.sfh.dog.entity.Daily;
import com.sfh.dog.entity.User;

/**
 * 个人记录
 * @author sWX229906
 *
 */
public class UserFragment extends AFragment
{
    private List<Daily> dailyEntities = new ArrayList<Daily>();
    private UserDailyAdapter adapter;
    @Override
    public void getData()
    {
        final User bmobUser = BmobUser.getCurrentUser(getActivity(),User.class);
        if (bmobUser != null)
        {
            BmobQuery<Daily> query = new BmobQuery<Daily>();
            query.addWhereEqualTo("dogID", bmobUser.getDogID());
            query.setSkip(10 * page);
            query.setLimit(10 * (page + 1));
            query.order("-createdAt");
            query.findObjects(getActivity(), new FindListener<Daily>()
            {
                
                @Override
                public void onSuccess(List<Daily> arg0)
                {
                    if (page == 0)
                    {
                        dailyEntities.clear();
                    }
                    dailyEntities.addAll(arg0);
                    adapter.notifyDataSetChanged();
                    mPullRefreshListView.onRefreshComplete();
                }
                
                @Override
                public void onError(int arg0, String arg1)
                {
                    mPullRefreshListView.onRefreshComplete();
                    
                }
            });
            
        }
   
        
    }

    @Override
    public void getInit()
    {
        adapter = new UserDailyAdapter(getActivity(), dailyEntities);
        mPullRefreshListView.setAdapter(adapter);
    }

    @Override
    public void select(int position)
    {
        Daily entity = dailyEntities.get(position - 1);
        DogApplication.gotoDailyDetailUI(getActivity(), entity);
        
    }

    @Override
    public int Layout()
    {
        return R.layout.usermain_fragment;
    }
    
}
